package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.ChangePublicPoolModel2SeniorDTO;
import com.xbongbong.paas.pojo.dto.ChangePublicPoolModel2StandardDTO;
import com.xbongbong.paas.pojo.dto.ChangePublicPoolModelStatusDTO;
import com.xbongbong.paas.pojo.dto.ChangeRuleStatusDTO;
import com.xbongbong.paas.pojo.dto.ConfirmChangePublicPoolModelDTO;
import com.xbongbong.paas.pojo.dto.GroupApplyScoreGetDTO;
import com.xbongbong.paas.pojo.dto.GroupObjectiveGetDTO;
import com.xbongbong.paas.pojo.dto.RemindTimeGetDTO;
import com.xbongbong.paas.pojo.dto.RemindTimeSaveDTO;
import com.xbongbong.paas.pojo.dto.TransferBackRuleSeniorDetailDTO;
import com.xbongbong.paas.pojo.dto.TransferBackRuleSeniorSaveDTO;
import com.xbongbong.paas.pojo.vo.ChangePublicPoolModelSeniorVO;
import com.xbongbong.paas.pojo.vo.ChangePublicPoolModelStatusVO;
import com.xbongbong.paas.pojo.vo.CustomerRuleRemindTimeVO;
import com.xbongbong.paas.pojo.vo.TransferBackRuleSeniorItemVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.TransferBackRuleSeniorService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ClueErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.publicgroup.pojo.vo.PublicGroupSeniorListVO;
import com.xbongbong.saas.help.PublicSeniorRuleHelp;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author yanglei
 * @ProjectName xbb-pro
 * @Description: 高级公海池规则相关数据接口
 * @date 2012/4/14 09:38
 */

@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/publicSeniorRule")
public class TransferBackRuleSeniorController {


    @Resource
    private TransferBackRuleSeniorService transferBackRuleSeniorService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private PublicSeniorRuleHelp publicSeniorRuleHelp;


    /**
     * 【web】保存接口
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid TransferBackRuleSeniorSaveDTO transferBackRuleSeniorListDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(transferBackRuleSeniorListDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(transferBackRuleSeniorListDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            //公海模式切换中校验
            if (!publicSeniorRuleHelp.checkChangeSeniorStatus(transferBackRuleSeniorListDTO.getCorpid(), transferBackRuleSeniorListDTO.getBusinessType())){
                response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                return JSON.toJSONString(response);
            }
            try {
                Integer save = transferBackRuleSeniorService.save(transferBackRuleSeniorListDTO);
                response = new XbbResponse<>(save);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】适用范围
     */
    @RequestMapping(value = "/group/applyScore/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String applyScoreGet(@RequestBody @Valid GroupApplyScoreGetDTO applyScoreGetDTO, BindingResult br) {
        XbbResponse<PublicGroupSeniorListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                 PublicGroupSeniorListVO groupSeniorListVO = transferBackRuleSeniorService.applyScoreGet(applyScoreGetDTO);
                 response = new XbbResponse<>(groupSeniorListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 【web】目标公海
     */
    @RequestMapping(value = "/group/objective/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String objectiveGet(@RequestBody @Valid GroupObjectiveGetDTO objectiveGetDTO, BindingResult br) {
        XbbResponse<PublicGroupSeniorListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PublicGroupSeniorListVO groupSeniorListVO = transferBackRuleSeniorService.groupObjectiveGet(objectiveGetDTO);
                response = new XbbResponse<>(groupSeniorListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 【web】整体界面
     */
    @RequestMapping(value = "/item", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String item(@RequestBody @Valid TransferBackRuleSeniorDetailDTO backRuleSeniorDetailDTO, BindingResult br) {
        XbbResponse<TransferBackRuleSeniorItemVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransferBackRuleSeniorItemVO seniorItemVO = transferBackRuleSeniorService.getTransferBackRuleItem(backRuleSeniorDetailDTO);
                response = new XbbResponse<>(seniorItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 修改公海规则的状态
     */
    @RequestMapping(value = "/changeRuleStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeRuleStatus(@RequestBody @Valid ChangeRuleStatusDTO statusDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(statusDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(statusDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(statusDTO.getCorpid(), statusDTO.getBusinessType())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                Integer update = transferBackRuleSeniorService.changeRuleStatus(statusDTO);
                response = new XbbResponse<>(update);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 公海高级和标准模式 - 提醒时间设置详情
     * @param remindTimeDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/getRemindTime", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRemindTime(@RequestBody @Valid RemindTimeGetDTO remindTimeDTO, BindingResult br) {
        XbbResponse<CustomerRuleRemindTimeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CustomerRuleRemindTimeVO ruleRemindTimeVO = transferBackRuleSeniorService.getRemindTime(remindTimeDTO);
                response = new XbbResponse<>(ruleRemindTimeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 公海高级和标准模式 - 提醒时间设置详情
     * @param remindTimeSaveDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/saveRemindTime", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveRemindTime(@RequestBody @Valid RemindTimeSaveDTO remindTimeSaveDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                //公海模式切换中校验
                if (!publicSeniorRuleHelp.checkChangeSeniorStatus(remindTimeSaveDTO.getCorpid(), remindTimeSaveDTO.getBusinessType())){
                    response = new XbbResponse<>(new XbbException(ClueErrorCodeEnum.API_ERROR_1111012));
                    return JSON.toJSONString(response);
                }
                Integer insert = transferBackRuleSeniorService.saveRemindTime(remindTimeSaveDTO);
                response = new XbbResponse<>(insert);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }



    /**
     * 高级模式切换接口
     */
    @RequestMapping(value = "/changePublicPoolModel/senior", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changePublicPoolModel2Senior(@RequestBody @Valid ChangePublicPoolModel2SeniorDTO changePublicPoolModel2SeniorDTO, BindingResult br) {
        XbbResponse<ChangePublicPoolModelSeniorVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(changePublicPoolModel2SeniorDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(changePublicPoolModel2SeniorDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                ChangePublicPoolModelSeniorVO baseVO = transferBackRuleSeniorService.changePublicPoolModel2Senior(changePublicPoolModel2SeniorDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 高级模式切换到标准模式下
     */
    @RequestMapping(value = "/changePublicPoolModel/standard", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changePublicPoolModel2Standard(@RequestBody @Valid ChangePublicPoolModel2StandardDTO changePublicPoolModel2StandardDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(changePublicPoolModel2StandardDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(changePublicPoolModel2StandardDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                BaseVO baseVO = transferBackRuleSeniorService.changePublicPoolModel2Standard(changePublicPoolModel2StandardDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取是否切换公海池完成
     */
    @RequestMapping(value = "/getChangePublicPoolStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getChangePublicPoolStatus(@RequestBody @Valid ChangePublicPoolModelStatusDTO changePublicPoolModelStatusDTO, BindingResult br) {
        XbbResponse<ChangePublicPoolModelStatusVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ChangePublicPoolModelStatusVO changePublicPoolModelStatusVO = transferBackRuleSeniorService.getChangePublicPoolStatus(changePublicPoolModelStatusDTO);
                response = new XbbResponse<>(changePublicPoolModelStatusVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 确认切换公海池完成
     */
    @RequestMapping(value = "/confirmChangePublicPoolComplete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String confirmChangePublicPoolComplete(@RequestBody @Valid ConfirmChangePublicPoolModelDTO confirmChangePublicPoolModelDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {

                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(confirmChangePublicPoolModelDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(confirmChangePublicPoolModelDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                BaseVO baseVO = transferBackRuleSeniorService.confirmChangePublicPoolComplete(confirmChangePublicPoolModelDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 非旗舰版切换公海池咨询客户生成一条销售机会
     */
    @RequestMapping(value = "/askPublicPoolSeniorModel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String askPublicPoolSeniorModel(@RequestBody @Valid BaseDTO baseDTO, BindingResult br){
        XbbResponse<BaseVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            BaseVO baseVO = null;
            try {
                baseVO = transferBackRuleSeniorService.askPublicPoolSeniorModel(baseDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
